import doms from "./doms.js";
import { login as apiLogin } from "./api/user.js";
let isLoginning = false; // 当前是否正在登录

export default async function () {
  if (isLoginning) {
    return; // 正在登录，直接结束
  }
  isLoginning = true;
  doms.btnSubmitEl.value = "登录中...";
  const loginIdVal = doms.userNameEl.value;
  const loginPwdVal = doms.userPasswordEl.value;

  if (!loginIdVal) {
    return alert("请填写账号");
  }

  if (!loginPwdVal) {
    return alert("请填写密码");
  }

  const resp = await apiLogin(loginIdVal, loginPwdVal);
  if (resp) {
    alert(`登录成功，欢迎你，${resp.nickname}`);
  } else {
    alert("登录失败");
  }
  isLoginning = false;
  doms.btnSubmitEl.value = "登录";
}
